package com.milks.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javassist.expr.NewArray;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import com.milks.bean.Pager;
import com.milks.bean.Pager.OrderType;
import com.milks.entity.Article;
import com.milks.entity.ArticleCategory;
import com.milks.entity.Brand;
import com.milks.entity.Product;
import com.milks.entity.ProductCategory;
import com.milks.service.ArticleCategoryService;
import com.milks.service.ArticleService;
import com.milks.service.BrandService;
import com.milks.service.ProductCategoryService;
import com.milks.service.ProductService;
import com.milks.utils.UploadPicture;

@Controller
@RequestMapping("/admin/article")
public class ArticleAdminController {
	
	@Resource
	private BrandService brandService;
	
	private static final long serialVersionUID = -6825456589196458406L;

	private Article article;
	private List<ArticleCategory> articleCategoryTreeList;

	@Resource
	private ArticleService articleService;
	@Resource
	private ArticleCategoryService articleCategoryService;
	
	@RequestMapping(value="/articleList",method=RequestMethod.GET)
	public String articleList(Model model,Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		pager = articleService.findByPager(pager);
		return "admin/article_list";
	}
	
	@RequestMapping(value="/articleAdd",method=RequestMethod.GET)
	public String addArticle(Model model) {
		article = new Article();
		article.setIsRecommend(false);
		article.setIsTop(false);
		article.setIsPublication(true);
		article.setArticleCategory(new ArticleCategory());
		List<ArticleCategory> articleCategoryTreeList = articleCategoryService.getArticleCategoryTreeList();
		model.addAttribute("articleCategoryTreeList", articleCategoryTreeList);
		model.addAttribute("article", article);
		model.addAttribute("isAdd", "add");
		return "admin/article_input";
	}
	
	@RequestMapping(value="/articleAdd",method=RequestMethod.POST)
	public String addArticle(Model model,@ModelAttribute Article article) {
		article.setHits(0);
		article.setCreateDate(new Date());
		articleService.save(article);
		return "redirect:articleList";
	}
	
	@RequestMapping(value="/articleUpdate",method=RequestMethod.GET)
	public String updateArticle(Model model,String id) {
		Brand brand =brandService.get(id);
		model.addAttribute("brand", brand);
		//是更新页面还是添加页面的标志
		model.addAttribute("isAdd", "edit");
		return "admin/brand_input";
	}
	
	@RequestMapping(value="/articleUpdate",method=RequestMethod.POST)
	public String updateArticle(Model model,Brand brand,HttpServletRequest request) {
		Brand persistent = brandService.get(brand.getId());
		BeanUtils.copyProperties(brand, persistent, new String[]{"id", "createDate", "modifyDate", "logo"});
		persistent.setLogo(brand.getLogo());
		brandService.update(persistent);
		return "redirect:brandList";
	}
	
	@RequestMapping(value="/articleDelete",method=RequestMethod.GET)
	@ResponseBody
	public HashMap<String, String> deleteArticle(Model model,String[] ids) {
		if (ids!=null) {
			for (int i = 0; i < ids.length; i++) {
				brandService.delete(ids[i]);
			}
		}
		HashMap<String, String> dataHashMap = new HashMap<String, String>();
		dataHashMap.put("status", "success");
		dataHashMap.put("message", "删除商品成功");
		return dataHashMap;
	}
}
